package com.ccc.boot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccc.boot.base.BaseServiceImpl;
import com.ccc.boot.constant.DictConstant;
import com.ccc.boot.exception.BusinessException;
import com.ccc.boot.pojo.DeptTree;
import com.ccc.boot.response.ResponseCode;
import com.ccc.boot.system.entity.SysDept;
import com.ccc.boot.system.mapper.SysDeptMapper;
import com.ccc.boot.system.pojo.SysDeptTree;
import com.ccc.boot.system.service.SysDeptService;
import com.ccc.boot.util.TreeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 〈部门——服务层〉
 *
 * @author superC
 * @version 0.1.0
 * @date 2022/3/29
 */
@Service
public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {

    @Override
    public SysDept getList(String deptName) {
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SysDept::getDeptName, deptName);
        SysDept sysDept = baseMapper.selectOne(wrapper);
        //数据未找到
        if (sysDept == null) {
            throw new BusinessException(ResponseCode.RESULT_DATA_NONE);
        }
        return sysDept;
    }

    /**
     * 获取有效部门列表
     *
     * @return {@link List< SysDept>}
     * @date 2022/3/31 16:06
     * @author superC
     */
    private List<SysDept> getEnabledMenus() {
        return baseMapper.getAll(DictConstant.STATUS_ENABLE);
    }

    @Override
    public List<SysDeptTree> getSysDeptTree() {
        List<SysDept> list = getEnabledMenus();
        // 转换Bean
        List<SysDeptTree> treeList = this.transProperties2(list);

        if (list.size() > 0) {
            return bulidSysDeptTree(treeList, TreeUtils.ROOT_NODE.longValue());
        }
        return null;
    }

    /**
     * 构建部门树
     *
     * @param treeNodes 传入的树节点列表
     * @param root      所需树的根节点
     * @return {@link List< SysDeptTree>}
     * @date 2022/4/1
     * @author superC
     */
    public static List<SysDeptTree> bulidSysDeptTree(List<SysDeptTree> treeNodes, Long root) {
        List<SysDeptTree> trees = new ArrayList<>();
        for (SysDeptTree treeNode : treeNodes) {
            // 根节点
            if (root.equals(treeNode.getPid())) {
                trees.add(treeNode);
            }
            for (SysDeptTree it : treeNodes) {
                if (it.getPid().equals(treeNode.getDeptCode())) {
                    treeNode.add(it);
                }
            }
        }
        return trees;
    }

    @Override
    public List<DeptTree> getDeptTree() {
        // 获取所有有效的部门
        List<SysDept> list = getEnabledMenus();
        // 转换Bean
        List<DeptTree> treeList = this.transProperties(list);

        // 整合tree
        return TreeUtils.bulid(treeList, TreeUtils.ROOT_NODE.longValue());
    }

    private List<DeptTree> transProperties(List<SysDept> list) {
        List<DeptTree> treeList = new ArrayList<>();
        list.forEach(dept -> {
            DeptTree tree = new DeptTree();
            BeanUtils.copyProperties(dept, tree);
            treeList.add(tree);
        });
        return treeList;
    }

    private List<SysDeptTree> transProperties2(List<SysDept> list) {
        List<SysDeptTree> treeList = new ArrayList<>();
        list.forEach(dept -> {
            SysDeptTree tree = new SysDeptTree();
            BeanUtils.copyProperties(dept, tree);
            treeList.add(tree);
        });
        return treeList;
    }

}
